#coding=utf8
from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render_to_response
from django.contrib import auth
from django.template import RequestContext
from django.contrib.auth.models import *

from django.contrib.auth.decorators import permission_required

from docshare.share.models import *
from docshare.share.tools import *
from docshare.share.forms import *

import settings

def index(request):
    return render_to_response('manage/index.html',context_instance=RequestContext(request))

def resource_sel(request):
    return render_to_response('manage/resource_sel.html',context_instance=RequestContext(request))

def setting_levelone(request):
    object_list = list(catalog.objects.filter(parentId=0))#取根目录
    if request.method == 'POST':
        chks = request.POST.getlist('chkID')
        for chk in chks:
            obj = catalog.objects.get(pk=chk)
            obj.delete()
        return HttpResponseRedirect('/%s/manage/setting_levelone/'%settings.SITE_ROOT)
    return render_to_response('manage/setting_levelone.html',{'object_list':object_list},context_instance=RequestContext(request))

def edit_levelone(request,id=0):
    if id!=0:
        obj = catalog.objects.get(pk=id)
        form = CataForm(instance=obj)
    else:
        obj = None
        form = CataForm()

    if request.method == 'POST':
        form = CataForm(request.POST,request.FILES,instance=obj)
        if form.is_valid():
            new_upCata=form.save()
            return HttpResponseRedirect('/%s/manage/setting_levelone/'%settings.SITE_ROOT)

    return render_to_response('manage/edit_levelone.html',{'form':form},context_instance=RequestContext(request))

def setting_leveltwo(request,id):
    obj = catalog.objects.get(pk=id)
    if request.method == 'POST':
        chks = request.POST.getlist('chkID')
        for chk in chks:
            obj = catalog.objects.get(pk=chk)
            obj.delete()
        return HttpResponseRedirect('/%s/manage/setting_leveltwo/%s'%(settings.SITE_ROOT,id))
    return render_to_response('manage/setting_leveltwo.html',{'obj':obj},context_instance=RequestContext(request))

def edit_leveltwo(request,pid=0,cid=0):
    if cid!='0':
        obj = catalog.objects.get(pk=cid)
        form = CataForm(instance=obj)
    else:
        obj = None
        form = CataForm()

    if request.method == 'POST':
        form = CataForm(request.POST,request.FILES,instance=obj)
        if form.is_valid():
            new_upCata=form.save(commit=False)
            new_upCata.parentId = catalog.objects.get(pk=pid)
            new_upCata.save()
            return HttpResponseRedirect('/%s/manage/setting_leveltwo/%s'%(settings.SITE_ROOT,pid))
    return render_to_response('manage/edit_leveltwo.html',{'form':form},context_instance=RequestContext(request))

def show_dictionary(request):
    objects = pagerTool(request,dictionaries.objects.all())
    root = dictionaries.objects.get(type__contains='root_path')
    if request.method == 'POST':
        chks = request.POST.getlist('chkID')
        for chk in chks:
            obj = dictionaries.objects.get(pk=chk)
            obj.delete()
        return HttpResponseRedirect('/%s/manage/show_dictionary/'%settings.SITE_ROOT)
    return render_to_response('manage/show_dictionary.html',{'objects':objects,'root':root},context_instance=RequestContext(request))


def update_root(request):
    if request.method == 'POST':
        obj = dictionaries.objects.get(type__contains='root_path')#设置根目录
        obj.name = request.POST['name']
        obj.save()
    return HttpResponseRedirect('/%s/manage/show_dictionary/'%settings.SITE_ROOT)

def edit_dictionary(request,id=0):
    if id==0:
        obj = None
        form = DictForm()
    else:
        obj = dictionaries.objects.get(pk=id)
        form = DictForm(instance=obj)

    if request.method == 'POST':
        form = DictForm(request.POST,instance=obj)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/%s/manage/show_dictionary/'%settings.SITE_ROOT)
    return render_to_response('manage/edit_dictionary.html',{'form':form},context_instance=RequestContext(request))


def clear(request):
    objects = pagerTool(request,upFile.objects.all())
    if request.method == 'POST':
        chks = request.POST.getlist('chkID')
        for chk in chks:
            obj = upFile.objects.get(pk=chk)
            obj.delete()
        return HttpResponseRedirect('/%s/manage/clear/'%settings.SITE_ROOT)
    return render_to_response('manage/clear.html',{'objects':objects},context_instance=RequestContext(request))

def edit_file(request,id):
    obj = upFile.objects.get(pk=id)
    if request.method == 'POST':
        form = UpFileForm(request.POST,request.FILES,instance=obj)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/%s/manage/clear/'%settings.SITE_ROOT)
    else:
        form = UpFileForm(instance=obj)
    return render_to_response('manage/edit_file.html',{'form':form},context_instance=RequestContext(request))


def show_roles(request):
    print request.user.get_all_permissions()
    objects = Group.objects.all()
    return render_to_response('manage/show_roles.html',{'objects':objects},context_instance=RequestContext(request))

def show_users(request):
    objects = pagerTool(request,User.objects.all())
    if request.method == 'POST':
        user_form = UserForm(request.POST)
        profile_form = ProfileForm(request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save(commit=False)
            user.set_password('000000')#设置加密后的初始密码
            user.is_staff = True
            user.is_active = True
            #user.user_permissions = user.get_group_permissions()
            user.save()
            user_form.save_m2m()
#            print user.user_permissions()
            profile = profile_form.save(commit=False)
            profile.user = user
            profile.save()
            return HttpResponseRedirect('/%s/manage/show_users/'%settings.SITE_ROOT)
    else:
        user_form = UserForm()
        profile_form = ProfileForm()
    return render_to_response('manage/show_users.html',{'objects':objects,'user_form':user_form,'profile_form':profile_form},context_instance=RequestContext(request))

def edit_active(request):
    if request.method == 'POST':
        chks = request.POST.getlist('chkID[]')
        for chk in chks:
            obj = User.objects.get(pk=chk)
            obj.is_active = not obj.is_active
            obj.save()
        return HttpResponseRedirect('/%s/manage/show_users/'%settings.SITE_ROOT)

def edit_user(request,id=0):
    if id!=0:
        obj = User.objects.get(pk=id)
        user_form = UserForm(instance=obj)
        profile_form = ProfileForm(instance=obj.profile)

    if request.method == 'POST':
        user_form = UserForm(request.POST,instance=obj)
        profile_form = ProfileForm(request.POST,instance=obj.profile)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save(commit=False)
            user.save()
            user_form.save_m2m()
            profile = profile_form.save()
            return HttpResponseRedirect('/%s/manage/show_users/'%settings.SITE_ROOT)
    return render_to_response('manage/show_users.html',{'obj':obj,'user_form':user_form,'profile_form':profile_form},context_instance=RequestContext(request))

def re_password(request,id):
    obj = User.objects.get(pk=id)
    obj.set_password('000000')#设置加密后的初始密码
    obj.save()
    return HttpResponseRedirect('/%s/manage/show_users/'%settings.SITE_ROOT)

def update_role(request,id=0):
    if id!=0:
        obj = Group.objects.get(pk=id)
        form = GroupForm(instance=obj)
    if request.method == 'POST':
        form = GroupForm(request.POST,instance=obj)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/%s/manage/show_roles/'%settings.SITE_ROOT)
    return render_to_response('manage/update_role.html',{'form':form},context_instance=RequestContext(request))


def no_permission(request):
    return render_to_response('Info.html',context_instance=RequestContext(request))

def trash_put(request,id):
    if request.method == 'POST':
        chks = request.POST.getlist('chkID[]')
        for chk in chks:
            obj = upFile.objects.get(pk=chk)
            obj.is_del = True
            obj.del_user = request.user
            obj.save()
        if id!='0':
            return HttpResponseRedirect('/%s/resource/show_files/%s'%(settings.SITE_ROOT,id))
    return HttpResponseRedirect('/%s/resource/search'%settings.SITE_ROOT)

def trash_del(request,id=0):
    if request.method == 'POST':
        chks = request.POST.getlist('chkID[]')
        for chk in chks:
            obj = upFile.objects.get(pk=chk)
            obj.delete()
        if id!='0':
            return HttpResponseRedirect('/%s/resource/show_files/%s'%(settings.SITE_ROOT,id))
        else:
            return HttpResponseRedirect('/%s/resource/trash/'%settings.SITE_ROOT)
    return HttpResponseRedirect('/%s/resource/trash/'%settings.SITE_ROOT)

def trash_revert(request):
    if request.method == 'POST':
        chks = request.POST.getlist('chkID[]')
        for chk in chks:
            obj = upFile.objects.get(pk=chk)
            obj.is_del = False
            obj.del_user = ''#还原后、删除者清空
            obj.save()
    return HttpResponseRedirect('/%s/resource/trash/'%settings.SITE_ROOT)

def delete_post(request,id):
    if request.user.is_superuser:
        obj = subject.objects.get(pk=id)
        obj.delete()
        return HttpResponseRedirect('/%s/forum/index/'%settings.SITE_ROOT)
    else:
        return HttpResponseRedirect('/%s/no_permission/'%settings.SITE_ROOT)

def delete_reply(request,id):
    if request.user.is_superuser:
        obj = reply.objects.get(pk=id)
        obj.delete()
        return HttpResponseRedirect('/%s/forum/show_replies/%s'%(settings.SITE_ROOT,obj.subject.id))
    else:
        return HttpResponseRedirect('/%s/no_permission/'%settings.SITE_ROOT)